<?php
/**
 * Created by JetBrains PhpStorm.
 * User: ncduc
 * Date: 7/1/13
 * Time: 10:14 PM
 * To change this template use File | Settings | File Templates.
 */
include_once("../libs/setup.php");
include_once("../libs/helper.php");
session_start();

if (!check_user_session()) {
    header("Location: ../signin.php");
    return;
}
$smarty->assign('wwwUrl','../');
function is_valid_education($id,$cur_userId, mysqli $mysqli)
{
    $query = "SELECT * from qua_trinh_dao_tao where Ma=$id";
    $result = $mysqli->query($query);
    if($result)
    {
        while ($row = $result->fetch_assoc()) {
            $id = $row['Ma'];
            $userId = $row['MaUser'];
            if($userId==$cur_userId)
            {
               return true;
            }
        }

    }
    return false;

}
function load_education_list(Mysqli $mysqli, $userId, Smarty $smarty)
{
    $query = "SELECT * FROM qua_trinh_dao_tao WHERE MaUser= {$userId}";
    $result = $mysqli->query($query);
    $arr_education = array();
    if ($result) {
        while ($row = $result->fetch_array(MYSQLI_BOTH)) {
            $arr_education[] = array('Ma' => $row['Ma'],
                'TenCoSoDaoTao_vi' => $row['TenCoSoDaoTao_vi'],
                'TenCoSoDaoTao_en' => $row['TenCoSoDaoTao_en'],
                'TuNgay' => $row['TuNgay'],
                'DenNgay' => $row['DenNgay'],
                'ChuyenNganh_vi' => $row['ChuyenNganh_vi'],
                'ChuyenNganh_en' => $row['ChuyenNganh_en'],
                'degree_en' => $row['CC_BC_en'],
                'degree_vi' => $row['CC_BC_vi'],
                'ChungChi_BangCap' => $row['ChungChi_BangCap']);
        }
    }
    if ($mysqli->error) {
        system_failed($mysqli->error, $smarty);
        $mysqli->close();
        return false;
    }
    $smarty->assign('educations', $arr_education);
    return true;
}

function get_education(Mysqli $mysqli, $id)
{
    $query = "SELECT * FROM qua_trinh_dao_tao WHERE Ma={$id}";
    $result = $mysqli->query($query);
    return $result;
}

assign_home_header($smarty);
//get data

$mysqli = connect_database($smarty);
$cur_userId = $_SESSION['ma_user'];
//if page_load
if (empty($_REQUEST['save']) && empty($_REQUEST['next'])) {

    if (!empty($_REQUEST['id']) && !empty($_REQUEST['action'])) {
        if ($_REQUEST['action'] == "edit") {
            $result = get_education($mysqli, $_REQUEST['id']);
            if ($result) {
                $have_result = false;
                while ($row = $result->fetch_assoc()) {
                    $have_result = true;
                    $id = $row['Ma'];
                    $userId = $row['MaUser'];
                    if($userId!=$cur_userId)
                    {
                        $message = "Không tìm thấy quá trình đào tạo";
                       $smarty->assign('is_display', "block");
                $smarty->assign('type_message','error');
                $smarty->assign('message', $message);
                    }
                    else
                    {
                        $institution_vi = $row['TenCoSoDaoTao_vi'];
                        $institution_en = $row['TenCoSoDaoTao_en'];
                        $address = $row['DiaChi'];
                        $fromDate = $row['TuNgay'];
                        $toDate = $row['DenNgay'];
                        $major_vi = $row['ChuyenNganh_vi'];
                        $major_en = $row['ChuyenNganh_en'];
                        $degree_en=$row['CC_BC_en'];
                        $degree_vi=$row['CC_BC_vi'];
                        $degree_uploaded = $row['ChungChi_BangCap'];
                        $smarty->assign('id', $id);
                        $smarty->assign('institution_vi', $institution_vi);
                        $smarty->assign('institution_en', $institution_en);
                        $smarty->assign('address', $address);
                        $smarty->assign('fromDate', $fromDate);
                        $smarty->assign('toDate', $toDate);
                        $smarty->assign('major_vi', $major_vi);
                        $smarty->assign('major_en', $major_en);
                        $smarty->assign('degree_vi', $degree_vi);
                        $smarty->assign('degree_en', $degree_en);
                        $smarty->assign('degree_uploaded', $degree_uploaded);
                    }

                }
                if (!$have_result) {
                    $message = "Không tìm thấy quá trình đào tạo";
                   $smarty->assign('is_display', "block");
                $smarty->assign('type_message','error');
                $smarty->assign('message', $message);

                }


            }
            if ($mysqli->error) {
                system_failed($mysqli->error, $smarty);
                $mysqli->close();
                return;
            }
        }
        if ($_REQUEST['action'] == "delete") {

            $id = $_REQUEST['id'];
            $sql = "DELETE FROM qua_trinh_dao_tao WHERE Ma={$id}";
            $mysqli->query($sql);
            if ($mysqli->error) {
                system_failed($mysqli->error, $smarty);
                $mysqli->close();
                return;
            }
        }
    }
    if (!load_education_list($mysqli, $cur_userId, $smarty)) {
        return;
    }
    $smarty->display('profile/2.tpl');
} else {
    if (isset($_REQUEST['save'])) {
        $id = addslashes($_REQUEST['id']);
        $institution_vi = addslashes($_REQUEST['institution_vi']);
        $institution_en = addslashes($_REQUEST['institution_en']);
        $address = addslashes($_REQUEST['address']);
        $fromDate = $_REQUEST['fromDate'];
        $toDate = $_REQUEST['toDate'];
        $major_vi = addslashes($_REQUEST['major_vi']);
        $major_en = addslashes($_REQUEST['major_en']);
        $degree_vi = addslashes($_REQUEST['degree_vi']);
        $degree_en = addslashes($_REQUEST['degree_en']);
        if (strlen($id) > 0) {

            $sql = "UPDATE qua_trinh_dao_tao SET TenCoSoDaoTao_vi='{$institution_vi}', TenCoSoDaoTao_en='{$institution_en}', DiaChi='{$address}', TuNgay=STR_TO_DATE('{$fromDate}','%m-%Y'), DenNgay=STR_TO_DATE('{$toDate}','%m-%Y'), ChuyenNganh_vi='{$major_vi}', ChuyenNganh_en='{$major_en}', CC_BC_vi='{$degree_vi}',CC_BC_en='{$degree_en}' WHERE Ma=$id";

        } else {
            $sql = "INSERT INTO qua_trinh_dao_tao (MaUser, TenCoSoDaoTao_vi, TenCoSoDaoTao_en, DiaChi, TuNgay, DenNgay, ChuyenNganh_vi, ChuyenNganh_en,`CC_BC_vi`,`CC_BC_en`) VALUES ({$cur_userId},'{$institution_vi}','{$institution_en}','{$address}',STR_TO_DATE('{$fromDate}','%m-%Y'),STR_TO_DATE('{$toDate}','%m-%Y'),'{$major_vi}','{$major_en}','{$degree_vi}','{$degree_en}')";
            $id = $mysqli->insert_id;

        }
        $result = $mysqli->query($sql);
        if ($mysqli->error) {
            system_failed($mysqli->error . "sql: " . $sql, $smarty);
            $mysqli->close();
            return;
        }
        if ($_FILES['degree_upload']['size']>0) {
            
            $upload_result = upload_file("degree_upload", $cur_userId);
            if (strlen($upload_result) > 0) //upload file error
            {
                $message = $upload_result;
               $smarty->assign('is_display', "block");
                $smarty->assign('type_message','error');
                $smarty->assign('message', $message);
            } else {
                $filename = $_FILES['degree_upload']['name'];
                $sql = "UPDATE qua_trinh_dao_tao SET ChungChi_BangCap = '{$filename}' WHERE Ma = {$id}";
                $result = $mysqli->query($sql);
                if ($mysqli->error) {
                    system_failed($mysqli->error . "sql: " . $sql, $smarty);
                    $mysqli->close();
                    return;
                }
            }
        }

        if (!load_education_list($mysqli, $cur_userId, $smarty)) {
            return;
        }
        $smarty->display('profile/2.tpl');
        $mysqli->close();
    } else //Next button clicked
    {
        header("Location: 3.php");
    }

}

